package team.seekers.oj.lesson.conventer;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import org.assertj.core.util.Lists;
import team.seekers.oj.common.converter.BaseConvertUtils;
import team.seekers.oj.lesson.dto.ChapterListDTO;

import java.io.*;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description DTO-DO 特殊转换方法的统一收口处
 * @author hbc
**/
public class ChapterConvertUtils extends BaseConvertUtils {

    public static List<Long> sectionsTo(byte[] sections) {
        if (sections == null) {
            return Lists.newArrayList();
        }
        ByteBuffer wrap = ByteBuffer.wrap(sections);
        List<Long> groupUserIdList = new ArrayList<>(sections.length / 8);
        for (int i = 0, n = sections.length; i < n; i += 8) {
            groupUserIdList.add(wrap.getLong(i));
        }
        return groupUserIdList;
    }

    public static byte[] sectionsFrom(List<Long> sectionIdList) {
        ByteBuf byteBuf = Unpooled.buffer(sectionIdList.size() * 8);
        sectionIdList.forEach(byteBuf::writeLong);
        return byteBuf.array();
    }

}